package com.library.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.library.bean.Admin;
import com.library.service.AdminService;
import com.library.service.impl.AdminServiceImpl;
@WebServlet("/login.do")
public class LoginAdminServlet extends HttpServlet{
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String adminName = req.getParameter("adminName");
		String passWord = req.getParameter("passWord");
		AdminService adminService = new AdminServiceImpl();
		Map<String,Object> map = new HashMap<String, Object>();
		map = adminService.loginAdmin(adminName, passWord);
		if(map.get("loginAdmin")!=null) {//登录成功，跳转到书籍界面
			HttpSession session = req.getSession();
			session.setAttribute("loginAdmin", (Admin)map.get("loginAdmin"));
			System.out.println(session.getAttribute("loginAdmin"));
			resp.sendRedirect("book.jsp");
		}else {//登录失败根据map携带信息给予提示
			req.setAttribute("msg", map.get("msg"));
			req.getRequestDispatcher("login.jsp").forward(req, resp);
		}
	}
}
